﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessModel;
using System.Data;
using System.Web.Security;

namespace PessMvc.Controllers
{
    [Authorize]
    public class ScoreController : Controller
    {
        //
        // GET: /Score/
        private PESSEntities db;

        public ScoreController()
        {
            db = new PESSEntities();
        }

        public ActionResult ArchiveExpertScores(int archiveID)
        {
            //Get Current User Name
            MembershipUser user=Membership.GetUser();
            if (Roles.IsUserInRole(user.UserName,"ExpertJudge"))
            {
                string expertSerialNumber = user.UserName;//"A1S7PW1";Membership

                var scores = db.sp_GetArchiveExpertScores(expertSerialNumber, archiveID).ToList();
                return View(scores);
            }
            else
            {
                return View();
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int ruleID, int archiveID)
        {
            //Get Current User Name
            string expertSerialNumber = Membership.GetUser().UserName;

            var score = (from s in db.Scores
                         where s.ArchiveID == archiveID && s.RuleID == ruleID && s.ExpertJudgeSerialNumber == expertSerialNumber
                         select s).SingleOrDefault();

            return View(score);
        }

        [HttpPost]
        public ActionResult Edit(Score score)
        {
            //Get Current User Name
            string expertSerialNumber = Membership.GetUser().UserName;//"A1S7PW1";
            score.ExpertJudgeSerialNumber = expertSerialNumber;

            if (ModelState.IsValid)
            {
                if (Helpers.Utility.ScoreValueIsLessthanLimit(score))
                {
                    var pessScore = db.Scores.Find(score.RuleID, score.ArchiveID, expertSerialNumber);
                    if (pessScore == null)
                    {
                        db.Scores.Add(score);
                    }
                    else
                    {
                        pessScore.Value = score.Value;
                        db.Entry(pessScore).State = EntityState.Modified;
                        //db.ObjectStateManager                    
                    }
                    db.SaveChanges();
                    return RedirectToAction("Details", "Archive", new { id = score.ArchiveID });
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "输入的分数高于规则限值！");
                }
            }
            return View(score);
        }



        public ActionResult EditScores(int archiveID)
        {
            MembershipUser user = Membership.GetUser();
            if (Roles.IsUserInRole(user.UserName, "ExpertJudge"))
            {
                string expertSerialNumber = user.UserName;//"A1S7PW1";Membership

                var scores = db.sp_GetArchiveExpertScores(expertSerialNumber, archiveID);
                return View(scores);
            }
            else
            {
                return View();
            }
        }

        public ActionResult EditScores2(int archiveID)
        {
            ViewBag.ArchiveID = archiveID;
            ViewBag.SerialNumber = db.Archives.Find(archiveID).SerialNumber;
            return EditScores(archiveID);
        }
        [HttpPost]
        public ActionResult SaveScores2(List<Score> scores)
        {
            //Get Current User Name
            string expertSerialNumber = Membership.GetUser().UserName;//"A1S7PW1";

            if (ModelState.IsValid)
            {                
                foreach (var score in scores)
                {
                    score.ExpertJudgeSerialNumber = expertSerialNumber;

                    if (Helpers.Utility.ScoreValueIsLessthanLimit(score))
                    {
                        var pessScore = db.Scores.Find(score.RuleID, score.ArchiveID, expertSerialNumber);
                        if (pessScore == null)
                        {
                            db.Scores.Add(score);
                        }
                        else
                        {
                            pessScore.Value = score.Value;
                            db.Entry(pessScore).State = EntityState.Modified;
                            //db.ObjectStateManager                    
                        }
                        db.SaveChanges();
                        //
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "输入的分数高于规则限值！");
                    }
                }
            }
            //Expert Judge don't really care about the details. So, let them see the index instead.
            //return RedirectToAction("Details", "Archive", new { id = scores.First().ArchiveID });  
            int archiveID=scores.First().ArchiveID;
            int activityID=db.Archives.Find(archiveID).Subject.ActivityID;
            return RedirectToAction("Index", "Archive", new { activityID = activityID });
        }
        [HttpPost]
        public ActionResult DeleteScores2(int archiveID)
        {
            DeleteScores(archiveID);
            //return RedirectToAction("Details", "Archive", new { id = archiveID });
            int activityID = db.Archives.Find(archiveID).Subject.ActivityID;
            return RedirectToAction("Index", "Archive", new { activityID = activityID });
        }

        [HttpPost]
        public string DeleteScores(int archiveID)
        {
            MembershipUser user = Membership.GetUser();
            if (Roles.IsUserInRole(user.UserName, "ExpertJudge"))
            {
                string expertSerialNumber = user.UserName;//"A1S7PW1";Membership
                var scores = db.Scores.Where(s => s.ArchiveID == archiveID && s.ExpertJudgeSerialNumber == expertSerialNumber).ToList();
                foreach (var s in scores)
                {
                    db.Scores.Remove(s);
                }
                db.SaveChanges();

                var comments = db.ExpertComments.Where(s => s.ArchiveID == archiveID && s.ExpertSerialNumber == expertSerialNumber).ToList();
                foreach (var c in comments)
                {
                    db.ExpertComments.Remove(c);
                }
                db.SaveChanges();
            }
            return "ok";
        }
    }
}
